package com.sreader.sreader4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.AbstractTextField;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.themes.BaseTheme;

public class FeuilleDeCalculIndividuelle extends CustomComponent{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Window mainWindow;
	private Locale currentLocale;
	private String currentLocaleValue;
	private AbsoluteLayout mainPanelAbsoluteLayout;
	private FeuilleDeIdentite feuilleIdentite;
	private FeuilleDeResultat feuilleDeResultat;
	private AbsoluteLayout formAbsoluteLayout;
	private AbsoluteLayout mainLayout;
	private PersonBean person;
	private Messages messages;
	private Panel mainPanel;
	private Form feuilleForm;
	private Button moreButton;
	private Button mailButton;
	private Button sendButton;
	private ClickListener sendButtonListener;
	private Button discardButton;
	private ClickListener discardButtonListner;
	private ClickListener printButtonListener;
	private ClickListener moreButtonListener;
	private Button printerButton;
	private Embedded faceBookButton;
	private MenuBar menubar;
	private Label horizontalBar;
	private Label occurenceTableCaption;
	private OccurenceTableUI occurenceTableUI;
	private ApplicationMenuBar menuBar;
	
	public FeuilleDeCalculIndividuelle(Locale defaultLocale, Window main){
		mainWindow = main;
		setCurrentLocale(defaultLocale);
		setMessages(new Messages("FeuilleDeCalcul",defaultLocale));
		person = new PersonBean();
		buildMainLayout();
		setCompositionRoot(mainLayout);
	}
	
	private Locale getCurrentLocale() {
		return currentLocale;
	}


	private void setCurrentLocale(Locale currentLocale) {
		this.currentLocale = currentLocale;
	}
	public Messages getMessages() {
		return messages;
	}

	public void setMessages(Messages messages) {
		this.messages = messages;
	}
	private AbsoluteLayout buildMainLayout() {
		//System.out.println( "Defaultlang is: " + getCurrentLocaleValue() );
		// common part: create layout
		mainLayout = new AbsoluteLayout();
		mainLayout.setCaption(this.messages.getString("feuilleDesc"));
		mainLayout.setImmediate(false);
		mainLayout.setWidth("725.0px"); 
		mainLayout.setHeight("920.0px"); 
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("750.0px"); 
		setHeight("950.0px");
		
		//ApplicationMenuBar applicationMenuBar = new ApplicationMenuBar(getCurrentLocaleValue());
		
		//menuBar= new ApplicationMenuBar(getCurrentLocale(), mainWindow);
		//mainLayout.addComponent(menuBar.getApplicationMenuBar(), "top:0.0px;left:10.0px;");	
		//options.addItem(caption, command)
		//mainLayout.addComponent(menubar);
		
		
		//horizontal bar
		HorizontalBar menuHorizontalBar = new HorizontalBar();
		menuHorizontalBar.setStyleName();
		menuHorizontalBar.setWidth("650.0px");
		mainLayout.addComponent(menuHorizontalBar, "top:23.0px;left:0.0px;");
		// mainPanel
		mainPanel = buildMainPanel();

		mainLayout.addComponent(mainPanel, "top:25.0px;left:0.0px;"); //$NON-NLS-1$
		
		return mainLayout;
	}
	public Panel buildMainPanel() {
		// common part: create layout
		mainPanel = new Panel(this.messages.getString("feuilleCaption"));
		mainPanel.setImmediate(false);
		mainPanel.setWidth("650.0px"); //720.0
		mainPanel.setHeight("320px"); //900.0
		
		// mainPanelAbsoluteLayout
		mainPanelAbsoluteLayout = buildMainPanelAbsoluteLayout();
		mainPanel.setContent(mainPanelAbsoluteLayout);
		
		return mainPanel;
	}
	private AbsoluteLayout buildMainPanelAbsoluteLayout() {
		feuilleForm = buildFeuilleForm();
		// common part: create layout
		mainPanelAbsoluteLayout = new AbsoluteLayout();
		mainPanelAbsoluteLayout.setImmediate(true);
	
		
		mainPanelAbsoluteLayout.setWidth("645.0px"); //715.0
		mainPanelAbsoluteLayout.setHeight("300.0px"); //870.0
		mainPanelAbsoluteLayout.setMargin(false);
		
		mainPanelAbsoluteLayout.addComponent(feuilleForm);
		
		return mainPanelAbsoluteLayout;
	}
	public Form buildFeuilleForm(){
		//common part. create layout
		feuilleForm = new Form();
		feuilleForm.setImmediate(true);
		//feuilleForm.setCaption(Messages.getString("FeuilleDeCalcul"+ getCurrentLocaleValue()+".properties.feuilleCaption")); //$NON-NLS-1$
		feuilleForm.setDescription(this.messages.getString("feuilleDescription"));
		feuilleForm.setImmediate(true);
		feuilleForm.setWidth("630.0px"); //710.0
		feuilleForm.setHeight("250px"); //860.0
		//feuilleForm.setWriteThrough(false);
			
		//formAbsoluteLayout
		formAbsoluteLayout = buildFormAbsoluteLayout();
		
		feuilleForm.setLayout(formAbsoluteLayout);
		
		return feuilleForm;
	}
	private AbsoluteLayout buildFormAbsoluteLayout(){
		formAbsoluteLayout = new AbsoluteLayout();
		formAbsoluteLayout.setImmediate(true);
		formAbsoluteLayout.setWidth("620.0px"); //700.0
		formAbsoluteLayout.setHeight("230px"); //840.0$NON-NLS-1$
		formAbsoluteLayout.setMargin(false);
		
		feuilleDeResultat = new FeuilleDeResultat(getCurrentLocale(),mainWindow);
		feuilleIdentite = new FeuilleDeIdentite(currentLocale, this.mainWindow);
		formAbsoluteLayout.addComponent(feuilleIdentite);
		sendButton =  new Button();
		//sendButton.setTabIndex(39);
		sendButton.setCaption(this.messages.getString("send"));
		sendButton.setDescription(this.messages.getString("send"));
		sendButton.setHeight("30px"); 
		sendButton.setWidth("90px"); 
		formAbsoluteLayout.addComponent(sendButton,
				"top:400.0px;left:500.0px;");
		sendButtonListener = new ClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				try{
					List<String> namesValuesidentity = feuilleIdentite.getNamesValues();
					List<String> firstnamesValuesIdentity = feuilleIdentite.getFirstnamesValues();
	
					feuilleIdentite.doNormalizeBirthDate();
					List<Integer> dateOfBirthIditenty = feuilleIdentite.getDateOfBirth();
					
					person.setIdentityValues(firstnamesValuesIdentity, namesValuesidentity,dateOfBirthIditenty);
					
					//Set the language of the people
					person.setLanguageCode(getCurrentLocale().getLanguage());
						
					person.init();
					person.computeNombreAbscents();
					person.computeNombresEnExces();
					person.computeNombreDominants();
					person.computeNombreActif();
					person.computeNombreHereditaire();
					person.computeNombreIntime();
					person.computeNombreDesInitiales();
					person.computeNombreDeRealisation();
					person.computeNombreDExpression();
					person.computeCheminDeVie();
					person.computeNombreUnique();
					person.computeAnneePersonnelle();
					person.computeNombreMoisPersonnelle();
					person.computeNombreJourPersonnel();
								
					
					//set the identity values for the result form
					feuilleDeResultat.setFirstNames(person.getFirstNames());
					feuilleDeResultat.setNames(person.getNames());
					feuilleDeResultat.setBirthDateAsString(person.getBirthDateAsString());
					//set the result of the calculation
					feuilleDeResultat.setTableDeResultat(person.getNombreActif(), 
							person.getNombreHereditaire(), person.getNombreDExpression(), 
							person.getNombreIntime(), person.getNombreDeRealisation(),
							person.getNombreDesInitiales(), person.getCheminDeVie(),
							person.getNombreUnique(),
							(ArrayList<Integer>)person.getNombreAbscents(), 
							(ArrayList<Integer>)person.getNombreDominants(),
							(ArrayList<Integer>)person.getNombresEnExces(),
							person.getAnneePersonnelle(),
							person.getMoisPersonnel(),
							person.getJourPersonnel());
					//occurenceTableUI
					occurenceTableUI.setOccurenceTableValues(person.getOccurenceTable());
					//resize the window
					Map <Component,Map<String,String>> componentsToResize = new HashMap<Component, Map<String,String>>();
					Map <String,String> newDimensionsFormLayout= new HashMap<String,String>();
					newDimensionsFormLayout.put("620.0px", "510.0px");
					componentsToResize.put(formAbsoluteLayout, newDimensionsFormLayout);
					
					Map <String,String> newDimensionsForm = new HashMap<String,String>();
					newDimensionsForm.put("630.0px", "530.0px");
					componentsToResize.put(feuilleForm, newDimensionsForm);
					
					Map <String,String> NewDimensionsMainPanelAbsoluteLayout = new HashMap<String,String>();
					NewDimensionsMainPanelAbsoluteLayout.put("645.0px", "540px");
					componentsToResize.put(mainPanelAbsoluteLayout, NewDimensionsMainPanelAbsoluteLayout);
					
					Map <String,String> NewDimensionsMainPanel = new HashMap<String,String>();
					NewDimensionsMainPanel.put("650px", "570px");
					componentsToResize.put(mainPanel, NewDimensionsMainPanel);
					
					resizeComponents(componentsToResize);
					
				}
				catch(Exception e){
					e.getMessage();
				}
					//enable the buttons
					enableComponents();
				}
			};
			sendButton.addListener(sendButtonListener);
			//feuille de résultat
			formAbsoluteLayout.addComponent(feuilleDeResultat, 
					"top:226.0px;left:60.0px;");

			//discardButton
			discardButton =  new Button(this.messages.getString("discard")); 
			discardButton.setDescription(this.messages.getString("discard"));
			discardButton.setTabIndex(40);
			discardButton.setHeight("30px"); //$NON-NLS-1$
			discardButton.setWidth("90px"); //$NON-NLS-1$
			formAbsoluteLayout.addComponent(discardButton,
					"top:160.0px;left:368.0px;"); //$NON-NLS-1$
			discardButtonListner = new ClickListener() {
				
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public void buttonClick(ClickEvent event) {
					// TODO Auto-generated method stub
					feuilleDeResultat.resetTableDeResultat();
				}
			};
			discardButton.addListener(discardButtonListner);
			//vertical bar
			Label verticalBar = new Label();
			verticalBar.addStyleName("vertical-separator");
			verticalBar.setWidth("1px");
			formAbsoluteLayout.addComponent(verticalBar, "top:20.0px;left:575.0px;");
			
			// moreButton
			moreButton = new Button();
			moreButton.setStyleName(BaseTheme.BUTTON_LINK);
			//moreButton.setCaption(Messages.getString(LOCALEBASE + getCurrentLocaleValue() +"."+ LOCALEPROPSTRING 
			//						+".moreButtonCapt"));
			moreButton.setDescription(this.messages.getString("moreButtonDesc"));
			moreButton.setIcon(new ThemeResource("img/zoom.png"));
			moreButton.setImmediate(true);
			formAbsoluteLayout.addComponent(moreButton, 
							"top:20.0px;left:585.0px;");
			moreButtonListener = new ClickListener() {
				
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public void buttonClick(ClickEvent event) {
					// TODO Auto-generated method stub
					
					setComponentVisibility(occurenceTableCaption, true);
					setComponentVisibility(occurenceTableUI, true);
					
					//resize the window
					Map <Component,Map<String,String>> componentsToResize = new HashMap<Component, Map<String,String>>();
					Map <String,String> newDimensionsFormLayout= new HashMap<String,String>();
					newDimensionsFormLayout.put("620.0px", "840.0px");
					componentsToResize.put(formAbsoluteLayout, newDimensionsFormLayout);
					
					Map <String,String> newDimensionsForm = new HashMap<String,String>();
					newDimensionsForm.put("630.0px", "860.0px");
					componentsToResize.put(feuilleForm, newDimensionsForm);
					
					Map <String,String> NewDimensionsMainPanelAbsoluteLayout = new HashMap<String,String>();
					NewDimensionsMainPanelAbsoluteLayout.put("645.0px", "870px");
					componentsToResize.put(mainPanelAbsoluteLayout, NewDimensionsMainPanelAbsoluteLayout);
					
					Map <String,String> NewDimensionsMainPanel = new HashMap<String,String>();
					NewDimensionsMainPanel.put("650px", "900px");
					componentsToResize.put(mainPanel, NewDimensionsMainPanel);
					
					resizeComponents(componentsToResize);
					
					feuilleIdentite.showMiseEnNombre(person);
				}
			};
			moreButton.addListener(moreButtonListener);
			//moreButtonSeparator
			HorizontalBar moreButtonSeparator = new HorizontalBar();
			moreButtonSeparator.setStyleName();
			moreButtonSeparator.setWidth("50.0px");
			formAbsoluteLayout.addComponent(moreButtonSeparator, 
					"top:60.0px;left:575.0px;");
			//mailButton
			mailButton = new Button();
			mailButton.setStyleName(BaseTheme.BUTTON_LINK);
			//mailButton.setCaption("senden");
			mailButton.setDescription(this.messages.getString("mailButtonDesc"));
			mailButton.setIcon(new ThemeResource("img/mail.jpg"));
			mailButton.setImmediate(true);
			formAbsoluteLayout.addComponent(mailButton, 
					"top:67.0px;left:585.0px;");
			//mailButtonSeparator
			HorizontalBar mailButtonSeparator = new HorizontalBar();
			mailButtonSeparator.setStyleName();
			mailButtonSeparator.setWidth("50.0px");
			formAbsoluteLayout.addComponent(mailButtonSeparator, 
					"top:100.0px;left:575.0px;");
			//printerButton
			printerButton = new Button();
			printerButton.setStyleName(BaseTheme.BUTTON_LINK);
			printerButton.setDescription("Print");
			//printerButton.setCaption("Drucken");
			printerButton.setIcon(new ThemeResource("img/print.png"));
			formAbsoluteLayout.addComponent(printerButton, 
					"top:105.0px;left:585.0px;");
			printButtonListener = new ClickListener() {
				
				@Override
				public void buttonClick(ClickEvent event) {
					// TODO Auto-generated method stub
					printerButton.getWindow().executeJavaScript("print();");
				}
			};
			printerButton.addListener(printButtonListener);
			//mailButtonSeparator
			HorizontalBar printerButtonSeparator = new HorizontalBar();
			printerButtonSeparator.setStyleName();
			printerButtonSeparator.setWidth("50.0px");
			formAbsoluteLayout.addComponent(printerButtonSeparator, 
					"top:140.0px;left:575.0px;");
			//secondVertical bar
			Label secondVerticalBar = new Label();
			secondVerticalBar.addStyleName("vertical-separator");
			secondVerticalBar.setWidth("1px");
			formAbsoluteLayout.addComponent(secondVerticalBar, "top:20.0px;left:619.0px;");
			
			
			//horizontal bar
			horizontalBar = new Label();
			horizontalBar.setStyleName("horizontal-separator");
			horizontalBar.setHeight("1px");
			formAbsoluteLayout.addComponent(horizontalBar, 
					"top:495.0px;left:0.0px;");
			
			//secondHorizontalBar 
			HorizontalBar secondHorizontalBar = new HorizontalBar();
			secondHorizontalBar.setStyleName();
			formAbsoluteLayout.addComponent(secondHorizontalBar, "top:200.0px;left:0.0px;");
			
			//occurenceTableCaption
			occurenceTableCaption = new Label();
			occurenceTableCaption.setContentMode(Label.CONTENT_RAW);
			occurenceTableCaption.setValue("<br>"+ this.messages.getString("occurenceTableCaption") +"</br>");
			formAbsoluteLayout.addComponent(occurenceTableCaption,
					"top:490.0px;left:350.0px;");
			occurenceTableCaption.setVisible(false);
			
			//occurenceTableUI
			occurenceTableUI = new OccurenceTableUI();
			formAbsoluteLayout.addComponent(occurenceTableUI, 
					"top:535.0px;left:350.0px;");
			occurenceTableUI.setVisible(false);
			
			//IdentityComponent
			//IdentityComponentUI myComponent = new IdentityComponentUI(getCurrentLocaleValue(),mainWindow);
			//formAbsoluteLayout.addComponent(myComponent, 
				//	"top:750.0px;left:10.0px;");
			
			disableComponents();
			return formAbsoluteLayout;

	}
	
	
	/**
	 * Cette methode recoit une liste de Componsants auquels sont attribués
	 * un couplet (lageur,hauteur) qui constitue les nouvelles dimensions
	 * qu'elle attribue à chacun des composants.
	 * @param newComponentDims
	 */
	public void resizeComponents(Map <Component, Map<String,String>> newComponentDims){
		
		for(Map.Entry<Component, Map<String,String>> aComponent : newComponentDims.entrySet()){
			Component theComponent = aComponent.getKey();
			Map <String,String> newDimensions = aComponent.getValue();
			System.out.println("Component " + theComponent);
			for(Map.Entry<String, String> aDimensionSet : newDimensions.entrySet()){
				String width = aDimensionSet.getKey();
				String height= aDimensionSet.getValue();
				theComponent.setWidth(width);
				theComponent.setHeight(height);
				System.out.println("W:H " + width +"|" + height);
			}
			theComponent.requestRepaint();
		}
	}
	/*
	 * 
	 */
	public void enableComponents(){
		moreButton.setEnabled(true);
		mailButton.setEnabled(true);
		printerButton.setEnabled(true);
	}
	/*
	 * 
	 */
	public void setComponentVisibility(Component component, boolean visible){
		//formAbsoluteLayout.getWindow().get
		component.setVisible(visible);
		component.getParent().requestRepaint();
		component.requestRepaint();
	}
	/**
	 * 
	 */
	public void disableComponents(){
		moreButton.setEnabled(false);
		mailButton.setEnabled(false);
		printerButton.setEnabled(false);
	}
}
